//
//  WSZLViewController.m
//  GameBaby
//
//  Created by keyZhang on 14-2-12.
//  Copyright (c) 2014年 keyZhang. All rights reserved.
//

#import "WSZLViewController.h"
#import "MoreTableHeaderView.h"
#import "WXSegment.h"
#import "DataService.h"
#import "AppDelegate.h"

@interface WSZLViewController ()
{
    MoreTableHeaderView *moreTbheadView;
    
    NSMutableDictionary *mDic;
    NSMutableDictionary *webmDic;
    
    UIButton *loadBtn;
    UIButton *webloadBtn;

    int rbSelected;
}

@end

@implementation WSZLViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"完善资料";
        self.navigationItem.titleView = t;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    mDic = [NSMutableDictionary dictionary];
    webmDic = [NSMutableDictionary dictionary];
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    moreTbheadView = [[[NSBundle mainBundle] loadNibNamed:@"MoreTableHeaderView" owner:self options:nil] lastObject];
    moreTbheadView.wszlBtn.hidden = YES;
    moreTbheadView.wszlLabel.hidden = YES;
    [self.wszlScrollView addSubview:moreTbheadView];
    
    [moreTbheadView addGestureRecognizer:tapRecognizer];

    
    NSArray *items = @[@"账号绑定",@"网站用户登陆"];

    WXSegment *segment = [[WXSegment alloc]initWithFrame:CGRectMake(20, moreTbheadView.bottom + 10, kScreenWidth-40, 35) items:items];
    segment.selectedIndex = 0;
    [segment addTarget:self action:@selector(segmentChange:) forControlEvents:UIControlEventValueChanged];
    
    [self.wszlScrollView addSubview:segment];
    
    
    //账号绑定视图
    self.zhbdView.frame = CGRectMake(20, segment.bottom, 280, 180);
    self.zhbdView.layer.borderColor = [UIColor colorWithWhite:0.869 alpha:1.000].CGColor;
    self.zhbdView.layer.borderWidth = 1;
    //创建单选按钮
    UIButton *rb1 = [UIButton buttonWithType:UIButtonTypeCustom];
    rb1.tag = 10;
    UIButton *rb2 = [UIButton buttonWithType:UIButtonTypeCustom];
    rb2.tag = 20;
    UIButton *rb3 = [UIButton buttonWithType:UIButtonTypeCustom];
    rb3.tag = 30;

    [rb1 setBackgroundImage:[UIImage imageNamed:@"RadioButton-Unselected.png"] forState:UIControlStateNormal];
    [rb1 setBackgroundImage:[UIImage imageNamed:@"RadioButton-Selected.png"] forState:UIControlStateSelected];
    [rb2 setBackgroundImage:[UIImage imageNamed:@"RadioButton-Unselected.png"] forState:UIControlStateNormal];
    [rb2 setBackgroundImage:[UIImage imageNamed:@"RadioButton-Selected.png"] forState:UIControlStateSelected];
    [rb3 setBackgroundImage:[UIImage imageNamed:@"RadioButton-Unselected.png"] forState:UIControlStateNormal];
    [rb3 setBackgroundImage:[UIImage imageNamed:@"RadioButton-Selected.png"] forState:UIControlStateSelected];
    [rb1 addTarget:self action:@selector(rbAction:) forControlEvents:UIControlEventTouchUpInside];
    [rb2 addTarget:self action:@selector(rbAction:) forControlEvents:UIControlEventTouchUpInside];
    [rb3 addTarget:self action:@selector(rbAction:) forControlEvents:UIControlEventTouchUpInside];
    
    rb1.frame = CGRectMake(55,45,22,22);
    rb2.frame = CGRectMake(115,45,22,22);
    rb3.frame = CGRectMake(165,45,22,22);
    
    [rb1 setSelected:YES];
    rbSelected = 1;

    [self.zhbdView addSubview:rb1];
    [self.zhbdView addSubview:rb2];
    [self.zhbdView addSubview:rb3];
    [self.wszlScrollView addSubview:self.zhbdView];
    
    self.userSecreatTXField.secureTextEntry = YES;
    self.wzUserSecreat.secureTextEntry = YES;

    self.wzLoginView.frame = self.zhbdView.frame;

    self.wzLoginView.layer.borderColor = [UIColor colorWithWhite:0.869 alpha:1.000].CGColor;
    self.wzLoginView.layer.borderWidth = 1;
    self.wzBg1.layer.borderColor = [UIColor colorWithWhite:0.869 alpha:1.000].CGColor;
    self.wzBg1.layer.borderWidth = 1;
    
    self.wzBg2.layer.borderColor = [UIColor colorWithWhite:0.869 alpha:1.000].CGColor;
    self.wzBg2.layer.borderWidth = 1;
    self.wzLoginView.hidden = YES;
    [self.wszlScrollView addSubview:self.wzLoginView];
    
    loadBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    loadBtn.frame = CGRectMake(110, self.zhbdView.bottom + 10, 100, 30);
    [loadBtn setTitle:@"完成提交" forState:UIControlStateNormal];
    [loadBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [loadBtn addTarget:self action:@selector(loadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.wszlScrollView addSubview:loadBtn];
    
    webloadBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    webloadBtn.hidden = YES;
    webloadBtn.frame = CGRectMake(110, self.zhbdView.bottom + 10, 100, 30);
    [webloadBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [webloadBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [webloadBtn addTarget:self action:@selector(webloadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.wszlScrollView addSubview:webloadBtn];
    
    self.wszlScrollView.contentSize = CGSizeMake(kScreenWidth, self.zhbdView.bottom +50);
    
    //判断是否绑定
    NSString *is_complete = [[NSUserDefaults standardUserDefaults] objectForKey:@"is_complete"];
    if ([is_complete intValue] == 1)
    {
        [loadBtn setTitle:@"注销" forState:UIControlStateNormal];
        self.userNameTXField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        self.userNameTXField.userInteractionEnabled = NO;
        self.userSecreatTXField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        self.userSecreatTXField.userInteractionEnabled = NO;
        self.userMailTXField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"];
        self.userMailTXField.userInteractionEnabled = NO;
        self.userPhoneNumTXField.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"tel"];
        self.userPhoneNumTXField.userInteractionEnabled = NO;
        
    };
    
    //判断是否网站登陆
    NSString *is_web_login = [[NSUserDefaults standardUserDefaults] objectForKey:@"is_web_login"];
    if ([is_web_login intValue] == 1)
    {
        [webloadBtn setTitle:@"注销" forState:UIControlStateNormal];
        self.wzUserEmail.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        self.wzUserEmail.userInteractionEnabled = NO;
        self.wzUserSecreat.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        self.wzUserSecreat.userInteractionEnabled = NO;
    };
    
}

/**
 *  性别单选
 *
 *  @param btn
 */
- (void)rbAction:(UIButton *)btn
{
    [btn setSelected:YES];
    if (btn.tag == 10) {
        [(UIButton *)[self.zhbdView viewWithTag:20] setSelected:NO];
        [(UIButton *)[self.zhbdView viewWithTag:30] setSelected:NO];
    }
    if (btn.tag == 20) {
        [(UIButton *)[self.zhbdView viewWithTag:10] setSelected:NO];
        [(UIButton *)[self.zhbdView viewWithTag:30] setSelected:NO];
    }
    if (btn.tag == 30) {
        [(UIButton *)[self.zhbdView viewWithTag:20] setSelected:NO];
        [(UIButton *)[self.zhbdView viewWithTag:10] setSelected:NO];
    }
    rbSelected = btn.tag / 10;
}


/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/**
 *  账号绑定
 *
 *  @param btn
 */


- (void)loadBtnClick:(UIButton *)btn
{
    __weak WSZLViewController *this = self;
    
    for (int i = 100; i<500; i+=100) {
        [(UITextField *)[self.zhbdView viewWithTag:i] resignFirstResponder];
    }
//    http://m.a.ewoka.com/register/complete
    
    if ([btn.titleLabel.text isEqualToString:@"完成提交"])
        {
            NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/register/complete"];
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",[mDic objectForKey:@"username"],@"username",[mDic objectForKey:@"password"],@"password",[NSString stringWithFormat:@"%d",rbSelected],@"sex",[mDic objectForKey:@"email"],@"email",[mDic objectForKey:@"tel"],@"tel", nil];
            
            [DataService requestWithURL:urlStr
                                 params:dic
                             httpMethod:@"POST"
                            finishBlock:^(id result) {
                                [this loadWSZLDataFinished:result];
                            }];
            
        }else if ([btn.titleLabel.text isEqualToString:@"注销"])
        {
            //http://m.a.ewoka.com/register/cancellation
            NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/register/cancellation"];
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",nil];
            
            [DataService requestWithURL:urlStr
                                 params:dic
                             httpMethod:@"POST"
                            finishBlock:^(id result) {
                                [this cancellWSZLDataFinished:result];
                            }];
        }
    
}

- (void)cancellWSZLDataFinished:(NSDictionary *)result
{
    if (result) {
        if ([[result objectForKey:@"reg"] intValue] == 0) {
            NSLog(@"%@",[NSUserDefaults standardUserDefaults]);
            [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"WSZLSuccessInfo"];
            [loadBtn setTitle:@"完成提交" forState:UIControlStateNormal];
            self.userNameTXField.text = @"";
            self.userSecreatTXField.text = @"";
            self.userMailTXField.text = @"";
            self.userPhoneNumTXField.text = @"";
            self.userPhoneNumTXField.userInteractionEnabled = YES;
            self.userNameTXField.userInteractionEnabled = YES;
            self.userSecreatTXField.userInteractionEnabled = YES;
            self.userMailTXField.userInteractionEnabled = YES;
            
            moreTbheadView.wjIDLabel.text = [NSString stringWithFormat:@"玩家ID：%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
            moreTbheadView.wjNameLabel.text = [NSString stringWithFormat:@""];
            moreTbheadView.wjWndLabel.text = [NSString stringWithFormat:@"剩余蜗牛蛋：%d个",0];
            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"username"];
            [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"wnd"];
            
            UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [msgAlert show];
            return;
        }
    }
    
    UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已注销" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [msgAlert show];
}

- (void)loadWSZLDataFinished:(NSDictionary *)result
{
    if (result) {
        if ([[result objectForKey:@"reg"] intValue] == 0) {
            [[NSUserDefaults standardUserDefaults] setObject:mDic forKey:@"WSZLSuccessInfo"];
            NSLog(@"%@",[NSUserDefaults standardUserDefaults]);
            [loadBtn setTitle:@"注销" forState:UIControlStateNormal];
            NSDictionary *wszlDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"WSZLSuccessInfo"];
            self.userNameTXField.text = [wszlDic objectForKey:@"username"];
            self.userNameTXField.userInteractionEnabled = NO;
            self.userSecreatTXField.text = [wszlDic objectForKey:@"password"];
            self.userSecreatTXField.userInteractionEnabled = NO;
            self.userMailTXField.text = [wszlDic objectForKey:@"email"];
            self.userMailTXField.userInteractionEnabled = NO;
            self.userPhoneNumTXField.text = [wszlDic objectForKey:@"tel"];
            self.userPhoneNumTXField.userInteractionEnabled = NO;
            
            [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"username"] forKey:@"username"];
            [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"uid"] forKey:@"uid"];
            [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"wnd"] forKey:@"wnd"];
            
            moreTbheadView.wjIDLabel.text = [NSString stringWithFormat:@"玩家ID：%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
            moreTbheadView.wjNameLabel.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"username"]];
            moreTbheadView.wjWndLabel.text = [NSString stringWithFormat:@"剩余蜗牛蛋：%@个",[[NSUserDefaults standardUserDefaults] objectForKey:@"wnd"]];
            //    设置头像
            NSString *strID2 = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
            NSString *urlID2 = [NSString stringWithFormat:@"http://m.avatar.ewoka.com/00%@/%@/%@/%@_avatar_middle.jpg",[strID2 substringToIndex:1],[strID2 substringWithRange:NSMakeRange(1, 2)],[strID2 substringWithRange:NSMakeRange(3, 2)],[strID2 substringFromIndex:5]];
            NSData *dataPic2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlID2]];
            [[NSUserDefaults standardUserDefaults] setObject:dataPic2 forKey:@"headImgData"];
            UIImage *newImg3 = [self circleImage:[UIImage imageWithData:dataPic2] withParam:1];
            [moreTbheadView.headImgBtn setBackgroundImage:newImg3 forState:UIControlStateNormal];


            UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [msgAlert show];
            
            [self viewWillAppear:YES];
            
            return;
        }else if ([[result objectForKey:@"reg"] intValue] == -10)
        {
            [loadBtn setTitle:@"注销" forState:UIControlStateNormal];
            UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@,请用网站用户登陆",[result objectForKey:@"msg"]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [msgAlert show];
            return;
        }
        
        UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [msgAlert show];
        
    }
    
}


/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/**
 *  网站用户登陆
 *
 *  @param btn
 */

- (void)webloadBtnClick:(UIButton *)btn
{
    [(UITextField *)[self.wzLoginView viewWithTag:500] resignFirstResponder];
    [(UITextField *)[self.wzLoginView viewWithTag:600] resignFirstResponder];
    //    http://m.a.ewoka.com/register/complete
    
    __weak WSZLViewController *this = self;
    if ([btn.titleLabel.text isEqualToString:@"登陆"]) {
        NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/register/link"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",[webmDic objectForKey:@"email"],@"email",[webmDic objectForKey:@"password"],@"password",nil];
        
        [DataService requestWithURL:urlStr
                             params:dic
                         httpMethod:@"POST"
                        finishBlock:^(id result) {
                            [this loadWZDataFinished:result];
                        }];
        
    }else if ([btn.titleLabel.text isEqualToString:@"注销"])
    {
        //http://m.a.ewoka.com/register/cancellation
        NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/register/cancellation"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",nil];
        
        [DataService requestWithURL:urlStr
                             params:dic
                         httpMethod:@"POST"
                        finishBlock:^(id result) {
                            [this cancellWZDataFinished:result];
                        }];

    }
    
}

- (void)cancellWZDataFinished:(NSDictionary *)result
{
    if ([[result objectForKey:@"reg"] intValue] == 0) {
        NSLog(@"%@",[NSUserDefaults standardUserDefaults]);
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"webSuccessInfo"];
        [webloadBtn setTitle:@"登陆" forState:UIControlStateNormal];
        
//        self.userNameTXField.text = @"";
//        self.userNameTXField.userInteractionEnabled = YES;
//        self.userSecreatTXField.text = @"";
//        self.userSecreatTXField.userInteractionEnabled = YES;
//        self.userMailTXField.text = @"";
//        self.userMailTXField.userInteractionEnabled = NO;
//        self.userPhoneNumTXField.text = @"";
//        self.userPhoneNumTXField.userInteractionEnabled = NO;
        
        self.wzUserEmail.text = @"";
        self.wzUserEmail.userInteractionEnabled = YES;
        self.wzUserSecreat.text = @"";
        self.wzUserSecreat.userInteractionEnabled = YES;
        
        moreTbheadView.wjIDLabel.text = [NSString stringWithFormat:@"玩家ID：%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
        moreTbheadView.wjNameLabel.text = [NSString stringWithFormat:@""];
        moreTbheadView.wjWndLabel.text = [NSString stringWithFormat:@"剩余蜗牛蛋：%d个",0];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"username"];
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"wnd"];
        
    }
    UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [msgAlert show];
}

- (void)loadWZDataFinished:(NSDictionary *)result
{

    NSLog(@"%@",result);
    if ([[result objectForKey:@"reg"] intValue] == 0) {
        [[NSUserDefaults standardUserDefaults] setObject:webmDic forKey:@"webSuccessInfo"];
        [webloadBtn setTitle:@"注销" forState:UIControlStateNormal];
        
//        self.wzUserEmail.text = @"";
//        self.wzUserEmail.userInteractionEnabled = NO;
//        self.wzUserSecreat.text = @"";
//        self.wzUserSecreat.userInteractionEnabled = NO;
        
        [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"username"] forKey:@"username"];
        [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"uid"] forKey:@"uid"];
        [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"wnd"] forKey:@"wnd"];
        if ((BOOL)[[result objectForKey:@"is_sign"]intValue]) {
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:1] forKey:@"is_sign"];
        }else
        {
            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:0] forKey:@"is_sign"];
        }

        
        moreTbheadView.wjIDLabel.text = [NSString stringWithFormat:@"玩家ID：%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
        moreTbheadView.wjNameLabel.text = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"username"]];
        moreTbheadView.wjWndLabel.text = [NSString stringWithFormat:@"剩余蜗牛蛋：%@个",[[NSUserDefaults standardUserDefaults] objectForKey:@"wnd"]];
        //    设置头像
        NSString *urlID = [result objectForKey:@"avatar"];
        NSData *dataPic = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlID]];
        [[NSUserDefaults standardUserDefaults] setObject:dataPic forKey:@"headImgData"];
        UIImage *newImg2 = [self circleImage:[UIImage imageWithData:dataPic] withParam:1];
        [moreTbheadView.headImgBtn setBackgroundImage:newImg2 forState:UIControlStateNormal];

    }
    UIAlertView *msgAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:[result objectForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [msgAlert show];
    
}


/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

//使img变圆形
-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {
    UIGraphicsBeginImageContext(image.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
	CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
	CGContextAddEllipseInRect(context, rect);
	CGContextClip(context);
	
	[image drawInRect:rect];
    CGContextAddEllipseInRect(context, rect);
	CGContextStrokePath(context);
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    for (int i = 100; i<500; i+=100) {
        [(UITextField *)[self.zhbdView viewWithTag:i] resignFirstResponder];
    }
    [(UITextField *)[self.wzLoginView viewWithTag:500] resignFirstResponder];
    [(UITextField *)[self.wzLoginView viewWithTag:600] resignFirstResponder];
    [self.wszlScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}


- (void)segmentChange:(WXSegment *)segment
{
    if (segment.selectedIndex == 0) {
        moreTbheadView.headImgBtn.userInteractionEnabled = YES;
        loadBtn.hidden = NO;
        self.wzLoginView.hidden = YES;
        webloadBtn.hidden = YES;
        loadBtn.tag = 700;
        [(UITextField *)[self.wzLoginView viewWithTag:500] resignFirstResponder];
        [(UITextField *)[self.wzLoginView viewWithTag:600] resignFirstResponder];
        
    }else
    {
        moreTbheadView.headImgBtn.userInteractionEnabled = NO;
        loadBtn.hidden = YES;
        webloadBtn.hidden = NO;
        self.wzLoginView.hidden = NO;
        loadBtn.tag = 701;
        for (int i = 100; i<500; i+=100) {
            [(UITextField *)[self.zhbdView viewWithTag:i] resignFirstResponder];
        }
//        [self.wszlScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma -mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag < 500) {
        self.wszlScrollView.contentSize = CGSizeMake(kScreenWidth, 600 + 150*568/kScreenHeight);
        [self.wszlScrollView setContentOffset:CGPointMake(0, textField.bottom*568/kScreenHeight + 100) animated:YES];
    }else
    {
        self.wszlScrollView.contentSize = CGSizeMake(kScreenWidth, 600 + 150*568/kScreenHeight);
        [self.wszlScrollView setContentOffset:CGPointMake(0, textField.bottom*568/kScreenHeight + 100 ) animated:YES];
    }
    
    
    return YES;
}


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 100:
            [mDic setObject:textField.text forKey:@"username"];
            break;
        case 200:
            [mDic setObject:textField.text forKey:@"password"];
            break;
        case 300:
            [mDic setObject:textField.text forKey:@"email"];
            break;
        case 400:
            [mDic setObject:textField.text forKey:@"tel"];
            break;
        case 500:
            [webmDic setObject:textField.text forKey:@"email"];
            break;
        case 600:
            [webmDic setObject:textField.text forKey:@"password"];
            break;
        default:
            break;
    }
    return YES;
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [(UITextField *)[self.wzLoginView viewWithTag:500] resignFirstResponder];
    [(UITextField *)[self.wzLoginView viewWithTag:600] resignFirstResponder];
    for (int i = 100; i<500; i+=100) {
        [(UITextField *)[self.zhbdView viewWithTag:i] resignFirstResponder];
    }
}


- (void)viewWillAppear:(BOOL)animated
{
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/user"];
    NSString *devicePlatForm = [(AppDelegate *)[UIApplication sharedApplication].delegate doDevicePlatform];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",devicePlatForm,@"client", nil];
    __weak WSZLViewController *this = self;
    [DataService requestWithURL:urlStr
                         params:dic
                     httpMethod:@"POST"
                    finishBlock:^(id result) {
                        [this loadDataFinished:result];
                    }];
}

- (void)loadDataFinished:(NSDictionary *)result
{
    NSLog(@"%@",result);
    [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"uid"] forKey:@"uid"];
    [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"username"] forKey:@"username"];
    [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"wnd"] forKey:@"wnd"];
    [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"is_complete"] forKey:@"is_complete"];
    [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"is_web_login"] forKey:@"is_web_login"];
    [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"is_sign"] forKey:@"is_sign"];
    
}

@end
